package com.hippomeetsskunk.savanna.types.person;

import java.util.HashMap;
import java.util.Map;

import com.hippomeetsskunk.savanna.model.ModelProvider;
import com.hippomeetsskunk.savanna.model.QualityModel;

public class Skills {

	public static final Skills NO_SKILL = new Skills();
	
	private Map<Skill, Double> skills = new HashMap<Skill, Double>();
	
	public Skills(){
	}
	
	public Skills(Skill skill, double value){
		skills.put(skill, value);
	}
	
	@SuppressWarnings("unchecked")
	public Skills(Map.Entry ... skills){
		for(Map.Entry e : skills){
			Skill s = (Skill) e.getKey();
			Double v = (Double) e.getValue();
			this.skills.put(s, v);
		}
	}
	
	/**
	 * how well does person satisfy requirements
	 * @param person
	 * @return
	 */
	public double getSkillFactor(Person person){
		Skills personSkills = person.getSkills();
		double f = 1.0;
		for(Skill s : skills.keySet()){
			double required = skills.get(s);
			double available = personSkills.get(s);
			final double basis = 1.0 + s.getBasis();
			double ratio = available / required;
			if (ratio <= 0.0) return 0.0;
			f *= Math.max(0.0, Math.log(ratio + 1.0) / Math.log(basis));
		}
		return f;
	}
	
	public void increaseSkill(Skill s, double v){
		
		final double current = skills.containsKey(s) ? skills.get(s) : 0.0;
		
		QualityModel model = ModelProvider.getInstance().getQualityModel();
		final double improved = model.improve(current, s.getDifficulty(), v);
		
		skills.put(s, improved);
	}
	
	public double get(Skill s){
		return (skills.containsKey(s)) ? skills.get(s) : 0.0;
	}
	
	public void set(Skill s, double v){
		skills.put(s, v);
	}
}
